package com.haha.gulimall.ware.service.impl;

import com.haha.common.utils.R;
import com.haha.gulimall.ware.feign.PurchaseServiceFeign;
import com.haha.common.to.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.Query;

import com.haha.gulimall.ware.dao.WareSkuDao;
import com.haha.gulimall.ware.entity.WareSkuEntity;
import com.haha.gulimall.ware.service.WareSkuService;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    PurchaseServiceFeign purchaseServiceFeign;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> qw = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        String wareId = (String) params.get("wareId");
        qw.eq(StringUtils.isNotEmpty(skuId),"sku_id",skuId)
                .eq(StringUtils.isNotEmpty(wareId),"ware_id",wareId);
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
               qw
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        List<WareSkuEntity> list = this.list(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(list==null || list.size()==0){
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setWareId(wareId);
            skuEntity.setStock(skuNum);
            skuEntity.setStockLocked(0);
            //获取sku的名字
            try{
                R info = purchaseServiceFeign.info(skuId);
                if(info.getCode()==0){
                    Map<String, Object> spuInfo = (Map<String, Object>) info.get("skuInfo");
                    skuEntity.setSkuName((String) spuInfo.get("skuName"));
                }
            }
            catch (Exception e){
                System.out.println("保存库存时获取skuname远程调用失败");
            }
            this.save(skuEntity);
        }
        else {
            //update
            this.baseMapper.addStock(skuId,wareId,skuNum);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {

        List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            Long count = this.baseMapper.getSkuHasStock(skuId);
            SkuHasStockVo vo = new SkuHasStockVo();
            vo.setSkuId(skuId);
            vo.setHasStock(count==null?false:count > 0);
            return vo;
        }).collect(Collectors.toList());
        return collect;

    }

}